#!/bin/bash
# $Id$
# vim: ts=2 syn=sh
# desc: host-initram/init for slackware


PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin ; export PATH
LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib ; export LD_LIBRARY_PATH

set -eE

_chk_err ()
{
		local count
		echo " ERROR:  6 beeps/seconds then reboot."
  	while true; do
  		echo -ne '\a'
  		sleep 1
			count=$(($count+1))
  		test $count -eq 6 && reboot -f
  	done
}

trap '_chk_err' 1 2 3 15 ERR

/bin/mount -t proc none /proc 
/bin/mount -n -t sysfs none /sys 
rm -f /etc/mtab
ln -s /proc/mounts /etc/mtab
ln -s /usr/bin/killall /bin/killall

/sbin/klogd -c 3

. /etc/scripts/env 
. /etc/scripts/utils.fn 
. /etc/scripts/boot.fn

export running_mode="hostinitram"
luks="cryptsetup"; export luks

setlogcons 12
setterm -blank 0
setterm -store

echo "##"
echo "## initram-${mostaza_version} ..."

_rescue () {
	if [ "$(cat /proc/cmdline|grep -i rescue)" ]; then
		echo ""
		echo " RESCUE MODE CALLED "; sleep 1
		echo ""
		rm -f /root/.bashrc
		ln -s /root/.bashrc.rescue /root/.bashrc
		/bin/bash --rcfile /root/.bashrc
		echo 1 > /tmp/rescue_mode
	fi 
}

# main
_rescue
if [ ! -f "/tmp/rescue_mode" ]; then
	_step_one
	_open_root
	_open_other_mps
	_kill_pass_prompt
fi


mount -o remount,rw,sync /mnt/root
if [ "$installer_for" = "gentoo" ]; then
	_set_devtar
elif [ "$installer_for" = "slackware" ]; then
	# see /etc/rc.d/rc.S
	mkdir -p -m 700 /mnt/root/etc/lvm/backup 
fi

/etc/rc.d/rc.udev force-stop 2>/dev/null || :

# starting the exodus
trap - ERR
umount -r -l -n /dev || :
mount -o remount,ro /mnt/root
umount /proc
umount /sys
set +eE
echo "##"
echo "## Calling /sbin/init ..."
exec switch_root /mnt/root /sbin/init $@
